#include "Utils.h"

Utils::Utils() {}

Utils::~Utils() {}

float Utils::ComputeDistance(vector<float> a, vector<float> b)
{
	int n = a.size();
	float sumSqr1 = 0;
	float sumSqr2 = 0;
	float sumA = 0;
	float sumB = 0;
	float mul = 0;

	for (int i = 0; i < n; i++)
	{
		sumA += a[i];
		sumB += b[i];

		sumSqr1 += a[i] * a[i];
		sumSqr2 += b[i] * b[i];
		
		mul += a[i] * b[i];
	}	
	float r = (n*mul - sumA*sumB) / sqrt((n*sumSqr1 - sumA*sumA) * (n*sumSqr2 - sumB*sumB));
	return r;
}

float Utils::CalCCOEF(CvMat* a, CvMat* b)
{
	int n = a->cols;
	float sumSqr1 = 0;
	float sumSqr2 = 0;
	float sumA = 0;
	float sumB = 0;
	float mul = 0;

	for (int i = 0; i < n; i++)
	{
		sumA += CV_MAT_ELEM(*a, float, 0, i);
		sumB += CV_MAT_ELEM(*b, float, 0, i);

		sumSqr1 += CV_MAT_ELEM(*a, float, 0, i)*CV_MAT_ELEM(*a, float, 0, i);
		sumSqr2 += CV_MAT_ELEM(*b, float, 0, i)*CV_MAT_ELEM(*b, float, 0, i);

		mul += CV_MAT_ELEM(*a, float, 0, i) * CV_MAT_ELEM(*b, float, 0, i);
	}
	float r = (n*mul - sumA*sumB) / sqrt((n*sumSqr1 - sumA*sumA) * (n*sumSqr2 - sumB*sumB));
	return r;
}